package com.symc.sims0213xfw.controller.user;

import com.symc.sims0213xfw.service.impl.UserServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * @Author: 凤文  沈阳医学院2019级医学信息工程 0213
 * @CreateTime: 2022/01/16 11:10
 * @Description:
 */
@WebServlet("/userRegisterServlet")
public class UserRegisterServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String uname = req.getParameter("uname");
        String upassword = req.getParameter("upassword");
        String confirm = req.getParameter("confirm");

        PrintWriter out = resp.getWriter();

        if (uname == null) {
            resp.sendRedirect(req.getContextPath()+"/index.jsp");
        } else if (uname.equals("") || upassword.equals("")) {
            out.write("您的用户名或密码为空，请重新输入！");
            out.write("2秒后自动返回注册页面。。。");
            resp.setHeader("refresh", "2;url=register.jsp");
        } else if (confirm.equals("")) {
            out.write("请确认您的密码！");
            out.write("2秒后自动返回注册页面。。。");
            resp.setHeader("refresh", "2;url=register.jsp");
        } else if (!confirm.equals(upassword)) {
            out.write("您输入的密码不匹配！");
            out.write("2秒后自动返回注册页面。。。");
            resp.setHeader("refresh", "2;url=register.jsp");
        } else {
            out.write(new UserServiceImpl().registerNewUserMessage(uname, upassword));
            out.write("2秒后自动返回登录页面。。。");
            resp.setHeader("refresh", "2;url=login.jsp");
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }

}
